//参考资料
//C++ 多线程编程实战
#include <iostream>

using namespace std;

// 在指定范围内生成一个随机数
double Rand(double dMin, double dMax)
{
	double dVal = (double)rand() / RAND_MAX;
	return dMin + dVal * (dMax - dMin);
}

//创建一个Complex类
class CComplex
{
public:
	CComplex()
	{
		dReal = Rand(-10, 10);
		dImg = Rand(-10, 10);
	}

	CComplex(double dReal, double dImg)
	{
		this->dReal = dReal;
		this->dImg = dImg;
	}

	// 方法必须声明为friend才能访问对象中的私有和保护成员
	friend CComplex operator+(const CComplex &c1, const CComplex &c2);
	friend ostream &operator<<(ostream &os, const CComplex &c);

private:
	double dReal;
	double dImg;
};

CComplex operator+(const CComplex &c1, const CComplex &c2)
{
	CComplex c(c1.dReal + c2.dReal, c1.dImg + c2.dImg);
	return c;
}

ostream &operator<<(ostream &os, const CComplex &c)
{
	return os << c.dReal << "+" << c.dImg << "i";
}

int main(int argc, char *argv[])
{
	CComplex c1;
	CComplex c2(-2.3, 0.9);
	CComplex c = c1 + c2;

	cout << c << endl;

	return 0;
}